package com.yalu.service.data.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yalu.dao.data.ProductyearsReportDao;
import com.yalu.service.data.ProductyearsReportService;
import com.yalu.util.ReportExportUtil;
import com.yalu.util.ReportHeaderUtil;

import core.report.yearReport.bean.BaseExcelReportDataBean;
import core.report.yearReport.bean.MergedBean;
import core.report.yearReport.bean.Productyears;
import core.report.yearReport.util.ExportExcelUtil;

@Service
public class ProductyearsReportServiceImpl implements ProductyearsReportService {

	@Resource
	private ProductyearsReportDao productyearsReportDao;

	@Override
	public void createReport(String dateType, String beginTime, String endTime,
			String resultFileName) {
		System.out.println("------------1");
		List<Productyears> dataList = productyearsReportDao.getReportDataBeanList(
				dateType, beginTime, endTime);
		System.out.println("------------2");
		// 平台总计
		List<BaseExcelReportDataBean> excelDataList = returnExcelDataList(
				dateType, dataList);
		System.out.println("------------3");
		List<MergedBean> headers = ReportHeaderUtil.getHeaders(dateType,
				beginTime, endTime, null);
		ExportExcelUtil ex = new ExportExcelUtil();
		OutputStream out;
		try {
			File file = new File(resultFileName);
			out = new FileOutputStream(file);
			ex.exportExcel(ReportHeaderUtil.getSheetNameByDateType(dateType),
					headers, excelDataList, out, ReportExportUtil
							.getBeginRowIndexByDateType(dateType),
					ReportHeaderUtil.getColCountByDateType(dateType), null);
			System.out.println("------------4");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	private List<BaseExcelReportDataBean> returnExcelDataList(String dateType,
			List<Productyears> dataList) {
		List<BaseExcelReportDataBean> excelDataList = new ArrayList<BaseExcelReportDataBean>();
		Productyears totalBean = new Productyears();// 品牌总计
		totalBean.setAttributeFlag(true);
		totalBean.setName("合计");
		for (int index = 0; index < dataList.size(); index++) {
			Productyears data = dataList.get(index);
			totalBean.addProperties(data);
			excelDataList.add(data);
		}
		totalBean.subtract(dataList.size());
		excelDataList.add(totalBean);
		return excelDataList;
	}
}
